flowchart TB
    %% User Interface Layer
    User[("👤 User<br/>Natural Language<br/>Tasks & Feedback")]
    
    %% Main Claude Code System
    subgraph ClaudeCode["🤖 Claude Code Agent System"]
        direction TB
        
        %% Core Dual Engine Architecture
        subgraph CoreEngine["Dual-Engine Architecture"]
            direction LR
            nO["nO Main Loop<br/>🔄 AgentLoop<br/>• Planning & Decision<br/>• State Management<br/>• Context Control"]
            h2A["h2A Async Queue<br/>📥 MessageQueue<br/>• Event Processing<br/>• Concurrent Handling<br/>• Real-time Steering"]
            
            nO <--> h2A
        end
        
        %% Hierarchical Layers
        subgraph Scheduler["Agent Core Scheduler Layer"]
            direction TB
            Perception["🧠 Perception Module<br/>• Context Understanding<br/>• State Estimation<br/>• Belief Updates"]
            Planning["📋 Planning Module<br/>• Task Decomposition<br/>• Option Selection<br/>• Strategy Generation"]
            Decision["⚡ Decision Module<br/>• Tool Selection<br/>• Parameter Optimization<br/>• Risk Assessment"]
            
            Perception --> Planning
            Planning --> Decision
        end
        
        subgraph ToolLayer["Tool Execution & Management Layer"]
            direction TB
            ToolEngine["🔧 Tool Engine<br/>• Tool Registry<br/>• Parameter Validation<br/>• Execution Control"]
            Concurrency["⚙️ Concurrency Manager<br/>• Parallel Execution<br/>• Resource Allocation<br/>• Conflict Resolution"]
            Security["🛡️ Security Gateway<br/>• Permission Control<br/>• Access Validation<br/>• Audit Logging"]
            
            ToolEngine --> Concurrency
            ToolEngine --> Security
        end
        
        %% Connections between layers
        CoreEngine --> Scheduler
        Scheduler --> ToolLayer
    end
    
    %% Mathematical Models
    subgraph MathModels["📊 Mathematical Foundation"]
        direction TB
        
        subgraph POMDP["POMDP Model"]
            States["S: Environment States<br/>📁 Codebase, Dependencies<br/>🧪 Test Coverage"]
            Actions["A: Action Space<br/>🔨 Tool Calls<br/>💻 Code Generation"]  
            Observations["O: Observations<br/>📄 Files, Logs<br/>📊 Test Results"]
            
            States -.-> Actions
            Actions -.-> Observations
            Observations -.-> States
        end
        
        subgraph Optimization["Multi-Objective Optimization"]
            Utility["U = w_Q·Quality - w_L·Latency<br/>    - w_C·Cost - w_R·Risk"]
            Constraints["Constraints:<br/>E[Latency] ≤ L_max<br/>E[Cost] ≤ C_max<br/>P(Risk > r*) ≤ ε"]
            
            Utility -.-> Constraints
        end
        
        subgraph Learning["Learning Models"]
            Bandit["Contextual Bandit<br/>🎯 Tool Selection<br/>📈 Exploration-Exploitation"]
            Submodular["Submodular Optimization<br/>📚 Context Selection<br/>🎛️ (1-1/e) Approximation"]
            Queue["Queue Theory<br/>⏱️ M/G/1 Analysis<br/>📊 Stability Conditions"]
            
            Bandit -.-> Submodular
            Submodular -.-> Queue
        end
    end
    
    %% Environment and Tools
    subgraph Environment["🌐 Development Environment"]
        direction TB
        FileSystem["📁 File System<br/>• Code Repository<br/>• Configuration Files<br/>• Build Artifacts"]
        Runtime["⚡ Runtime Environment<br/>• Execution Context<br/>• Process Management<br/>• System Resources"]
        External["🔗 External Services<br/>• Package Managers<br/>• CI/CD Systems<br/>• Code Quality Tools"]
        
        FileSystem <--> Runtime
        Runtime <--> External
    end
    
    subgraph Tools["🛠️ Tool Ecosystem"]
        direction TB
        FileOps["📂 File Operations<br/>• Read/Write/Edit<br/>• Search & Navigate<br/>• Version Control"]
        CodeTools["💻 Code Tools<br/>• Compilation<br/>• Testing<br/>• Linting/Formatting"]
        SystemTools["🖥️ System Tools<br/>• Shell Commands<br/>• Process Control<br/>• Environment Mgmt"]
        
        FileOps <--> CodeTools
        CodeTools <--> SystemTools
    end
    
    %% Verification and Quality
    subgraph Verification["✅ Verification & Quality"]
        direction TB
        Testing["🧪 Automated Testing<br/>• Unit Tests<br/>• Integration Tests<br/>• Regression Tests"]
        Static["📐 Static Analysis<br/>• Type Checking<br/>• Code Quality<br/>• Security Scan"]
        Formal["🔍 Formal Verification<br/>• Property Checking<br/>• Model Validation<br/>• Safety Guarantees"]
        
        Testing <--> Static
        Static <--> Formal
    end
    
    %% Main Flow Connections
    User <--> CoreEngine
    
    %% Tool Layer to Environment
    ToolLayer <--> Environment
    ToolLayer <--> Tools
    ToolLayer <--> Verification
    
    %% Mathematical Models inform Decision Making
    MathModels -.-> Decision
    MathModels -.-> ToolEngine
    MathModels -.-> Concurrency
    
    %% Feedback Loops
    Verification --> h2A
    Tools --> h2A
    Environment --> Perception
    
    %% Theoretical Properties (shown as annotations)
    subgraph Properties["🎯 Theoretical Guarantees"]
        direction TB
        Monotonic["Monotonic Improvement<br/>📈 Hierarchical Strategy"]
        Approximation["(1-1/e) Approximation<br/>📚 Context Selection"]
        Regret["O(d√T log T) Regret<br/>🎯 Tool Selection"]
        Stability["λE[S] < 1 Stability<br/>⏱️ Queue System"]
    end
    
    Properties -.-> MathModels
    
    %% Styling
    classDef userClass fill:#e1f5fe,stroke:#0277bd,stroke-width:2px
    classDef coreClass fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
    classDef mathClass fill:#e8f5e8,stroke:#388e3c,stroke-width:2px
    classDef toolClass fill:#fff3e0,stroke:#f57c00,stroke-width:2px
    classDef envClass fill:#fce4ec,stroke:#c2185b,stroke-width:2px
    classDef propClass fill:#f1f8e9,stroke:#689f38,stroke-width:2px
    
    class User userClass
    class ClaudeCode,CoreEngine,Scheduler,ToolLayer coreClass
    class MathModels,POMDP,Optimization,Learning mathClass
    class Tools,FileOps,CodeTools,SystemTools toolClass
    class Environment,FileSystem,Runtime,External,Verification envClass
    class Properties propClass